package com.shizhanzhe.qdd.forward.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.base.UserBean;
import com.shizhanzhe.qdd.forward.adapter.ShareAdapter;
import com.shizhanzhe.qdd.forward.bean.ShareList;
import com.shizhanzhe.qdd.home.activity.LoginActivity;
import com.shizhanzhe.qdd.url.Path;
import com.shizhanzhe.qdd.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.qdd.widget.LineEditText;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zz9527 on 2018/12/19.
 */
public class SearchActivity extends AppCompatActivity {

    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.et_coin)
    LineEditText etCoin;
    @BindView(R.id.go)
    ImageView go;
    @BindView(R.id.id_flowlayout)
    TagFlowLayout idFlowlayout;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    private String[] mVale = {"生活", "军事", "情感", "养生"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_goods);
        ImmersionBar.with(this).statusBarColor(R.color.them).fitsSystemWindows(true)
                .init();
        ButterKnife.bind(this);
        final LayoutInflater mInflater = LayoutInflater.from(this);
        idFlowlayout.setAdapter(new TagAdapter<String>(mVale) {
            @Override
            public View getView(FlowLayout parent, int position, String o) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tv2, parent, false);
                tv.setText(o);
                return tv;
            }
        });
        idFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                etCoin.setText(mVale[position]);
                getData();
                ll.setVisibility(View.VISIBLE);
                ll1.setVisibility(View.GONE);
                return true;
            }
        });


        etCoin.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    String key = etCoin.getText().toString().trim();
                    if (TextUtils.isEmpty(key)) {
                        Toast.makeText(SearchActivity.this, "搜索不能为空", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    getData();
                    ll.setVisibility(View.VISIBLE);
                    ll1.setVisibility(View.GONE);
                }
                return false;
            }
        });
    }


    @OnClick({R.id.iv_close, R.id.go})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.go:
                if (!TextUtils.isEmpty(etCoin.getText().toString())) {
                    getData();
                    ll.setVisibility(View.VISIBLE);
                    ll1.setVisibility(View.GONE);
                } else {
                    ll.setVisibility(View.GONE);
                    ll1.setVisibility(View.VISIBLE);
                    Toast.makeText(this, "搜索不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    int page = 1;
    List<ShareList.DataBean> list = new ArrayList<>();

    private void getData() {
        list.clear();
        OkHttpDownloadJsonUtil.downloadJson(this, Path.SHARESEARCH(etCoin.getText().toString(), page), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                try {
                    Gson gson = new Gson();
                    ShareList shareList = gson.fromJson(json, ShareList.class);
                    List<ShareList.DataBean> lists = shareList.getData();
                    if (lists.size() > 0) {
                        list.addAll(lists);
                        lv.setAdapter(new ShareAdapter(list, SearchActivity.this));
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                if (UserBean.uid != 0) {
                                    Intent intent = new Intent(SearchActivity.this, WebActivity.class).putExtra("id", list.get(i).getArtid());
                                    startActivity(intent);
                                } else {
                                    startActivity(new Intent(SearchActivity.this, LoginActivity.class));
                                }
                            }
                        });
                    }else {
                        Toast.makeText(SearchActivity.this, "暂无结果", Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {
                    Toast.makeText(SearchActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
